<?php
	class SinaWeibo{
		const WB_AKEY = "3307713508";
		const WB_SKEY = "04f75e8485f8e9eaadd64319b70e3f25";
		const backurl = "http://onpsummer.org/weixinInterface/redirect.php?url=http://activity.onpsummer.org/Game/sinaBind";
		//绑定url
		public function bind(){
			$rand = mt_rand();
			$agent = strtolower($_SERVER['HTTP_USER_AGENT']);
			if(strpos($agent,"mobile")){
				$display = "mobile";
				$u = "https://open.weibo.cn/oauth2/authorize";
			}else{
				$display ="default";
				$u = "https://api.weibo.com/oauth2/authorize";
			}
			$url = $u."?client_id=".self::WB_AKEY."&redirect_uri=".self::backurl."&scope=all&state=".$rand."&display=".$display;
			return $url;
		}
		//获取token
		public function getSinaToken($code){
			$post = "client_id=".self::WB_AKEY."&client_secret=".self::WB_SKEY."&grant_type=authorization_code&code=".$code."&redirect_uri=".self::backurl;
			$curl = curl_init();
			curl_setopt($curl, CURLOPT_URL, 'https://api.weibo.com/oauth2/access_token');
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt ($curl, CURLOPT_TIMEOUT, 20);
			curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36');
			//curl_setopt ( $curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
			curl_setopt ( $curl, CURLOPT_ENCODING, "" );
			curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
			curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
			curl_setopt ( $curl, CURLOPT_HEADER, FALSE );
			curl_setopt ( $curl, CURLINFO_HEADER_OUT, TRUE );
			curl_setopt ( $curl, CURLOPT_POST, TRUE );
			curl_setopt ( $curl, CURLOPT_POSTFIELDS, $post );
			$result = curl_exec($curl);
			echo curl_error($curl);
			curl_close($curl);
			return $result;
		}
		public function put($arr){
			$post ='access_token='.$arr["token"].'&status='.$arr["content"].'&id='.$arr["id"].'&is_comment=2&rip='.$arr["ip"];
			$curl = curl_init();
			curl_setopt($curl, CURLOPT_URL, 'https://api.weibo.com/2/statuses/repost.json');
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt ($curl, CURLOPT_TIMEOUT, 20);
			curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36');
			curl_setopt ( $curl, CURLOPT_ENCODING, "" );
			curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
			curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
			curl_setopt ( $curl, CURLOPT_HEADER, FALSE );
			curl_setopt ( $curl, CURLINFO_HEADER_OUT, TRUE );
			curl_setopt ( $curl, CURLOPT_POST, TRUE );
			curl_setopt ( $curl, CURLOPT_POSTFIELDS, $post);
			$results = curl_exec($curl);
			echo curl_error($curl);
			curl_close($curl);
			$result = json_decode($results,true);
			if($result["text"]==$arr['content']){
				return true;
			}elseif($result['error']=='invalid_access_token'||$result['error']=='expired_token'){
				return -2;
			}elseif($result['error']=='repeat content!'){
				return -3;
			}else{
				return false;
			}
		}
	}